// src/main.cpp
#include "random_text.h"
#include "fuzzy_matching.h"
#include <iostream>
#include <string>
#include <clocale>

#ifdef _WIN32
#include <windows.h>
#endif

int main() {
    // 设置全局locale为系统默认，以支持UTF-8
    std::setlocale(LC_ALL, "");

#ifdef _WIN32
    // Windows控制台需要启用UTF-8输出
    SetConsoleOutputCP(CP_UTF8);
#endif

    // 加载拼音映射文件
    std::string pinyin_map_file = "D:/OpenHarmony/OpenHarmony/pinyin_map.txt";
    if (!load_pinyin_map(pinyin_map_file)) {
        std::cerr << "加载拼音映射文件失败。" << std::endl;
        return -1;
    }

    // 创建并训练马尔可夫模型
    MarkovModel model(2); // 使用2-gram
    std::string corpus_file = "D:/OpenHarmony/OpenHarmony/chinese_corpus_tokenized.txt";
    if (!model.train(corpus_file)) {
        std::cerr << "训练马尔可夫模型失败。" << std::endl;
        return -1;
    }

    // 生成随机句子
    unsigned int seed_value = 666; // 随机数种子
    std::string generated_sentence = generate_random_sentence(model, 20, seed_value);
    std::cout << "生成的随机句子: " << generated_sentence << std::endl;

    // 转换生成的句子为拼音
    std::string original_pinyin = text_to_pinyin(generated_sentence);
    std::cout << "原始拼音: " << original_pinyin << std::endl;

    // 识别的内容
    std::string recognized = "从字能搜机到冷工智能，从太空探索到新能源开发，科技的进步也需要冷类的字慧"; // 示例识别结果（最终需要调用语音识别库）
    std::string recognized_pinyin = text_to_pinyin(recognized);
    std::cout << "识别拼音: " << recognized_pinyin << std::endl;

    // 计算相似度、编辑距离、长度差异
    double similarity = calculate_similarity(original_pinyin, recognized_pinyin);
    int edit_distance = calculate_edit_distance(original_pinyin, recognized_pinyin);
    double length_diff_ratio = 0.0;
    if (!original_pinyin.empty()) {
        length_diff_ratio = std::abs(static_cast<int>(original_pinyin.size()) -
            static_cast<int>(recognized_pinyin.size())) /
            static_cast<double>(original_pinyin.size());
    }

    std::cout << "相似度: " << similarity << std::endl;
    std::cout << "编辑距离: " << edit_distance << std::endl;
    std::cout << "长度差异比例: " << length_diff_ratio << std::endl;

    // 判断是否匹配
    bool match = is_match(generated_sentence, recognized, 0.6, 15, 0.15); // 根据需求调整阈值
    std::cout << "匹配结果: " << (match ? "匹配成功" : "匹配失败") << std::endl;

    return 0;
}
